#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Premiere.Framework;
using System.Collections;
using Premiere.DTO;
using Premiere.Window;

#endregion

namespace PremiereMX
{
    public partial class UserControlCurrency : UserControl, IUserControlCurrency
    {
        CurrencyLogic m_logic;
        IMainForm m_mainForm;
        decimal m_sample = 0.0100000000m;
        decimal m_decimalsample = 0.1234567890m;
        Hashtable m_items = new Hashtable();
        bool b_init = false;
        public UserControlCurrency(string name, IMainForm mainForm)
        {
            InitializeComponent();
            m_logic = new CurrencyLogic(mainForm, this);
            m_mainForm = mainForm;
            this.Name = name;
            this.Dock = DockStyle.Fill;
            InitButtonClick();
        }
        private void InitButtonClick()
        {
            toolBarButtonSave.Click += new EventHandler(toolBarButtonSave_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            toolBarButtonNew.Click += new EventHandler(toolBarButtonNew_Click);
            toolBarButtonPrint.Click += new EventHandler(toolBarButtonPrint_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);

            buttonSave.Click += new EventHandler(toolBarButtonSave_Click);
            buttonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            buttonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            buttonNew.Click += new EventHandler(toolBarButtonNew_Click);
        }
        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        void toolBarButtonPrint_Click(object sender, EventArgs e)
        {
            m_logic.Print();
        }

        void toolBarButtonNew_Click(object sender, EventArgs e)
        {
            m_logic.New();
        }

        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            m_logic.Edit();
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Are you sure to delete this record?", "Confirmation", MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question, new EventHandler(DeleteConfirmation));
        }
        void DeleteConfirmation(object sender, EventArgs e)
        {
            MessageBoxWindow msg = (MessageBoxWindow)sender;
            if (msg.DialogResult == DialogResult.Yes)
                m_logic.Delete();
        }


        void toolBarButtonSave_Click(object sender, EventArgs e)
        {
            m_logic.Save();
        }
        private void listViewRecords_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        #region IUserControlCurrency Members

        public string Code
        {
            get
            {
                return textBoxCode.Text;
            }
            set
            {
                textBoxCode.Text = value;
            }
        }

        public string Named
        {
            get
            {
                return textBoxName.Text;
            }
            set
            {
                textBoxName.Text = value;
                tabPage1.Text = Code +" - "+ value;
            }
        }

        public string Symbol
        {
            get
            {
                return textBoxSymbol.Text;
            }
            set
            {
                textBoxSymbol.Text = value;
            }
        }

        public int DecimalPlace
        {
            get
            {
                return Convert.ToInt16(numericUpDownDecimalPlace.Value);
            }
            set
            {
                numericUpDownDecimalPlace.Value = value;
            }
        }

        public RoundingType RoundingType
        {
            get
            {
                return (RoundingType)comboBoxRoundingType.SelectedItem;
            }
            set
            {
                comboBoxRoundingType.SelectedItem = value;
            }
        }

        public int AutoAdjustment
        {
            get
            {
                return Convert.ToInt16(numericUpDownAutoAdjustment.Value);
            }
            set
            {
                numericUpDownAutoAdjustment.Value = value;
            }
        }

        public bool IsDefault
        {
            get
            {
                return checkBoxIsDefault.Checked;
            }
            set
            {
                checkBoxIsDefault.Checked = value;
            }
        }

        public DateTime LastUpdate
        {
            set { statusBar1.Text = statusBar1.Text + " Last Updated on : " + value.ToString("dd-MMM-yyyy"); }
        }

        public string ComputerName
        {
            set { statusBar1.Text = statusBar1.Text + " Computer Name : " + value; }
        }

        public string UserUpdateBy
        {
            set { statusBar1.Text = statusBar1.Text + " Updated By : " + value; }
        }

        public void ShowMessage(string mess)
        {
            MessageBox.Show(mess, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public void UpdateList(IList list)
        {
            foreach (CurrencyDTO curr in list)
            {
                if (m_items.ContainsKey(curr.Id))
                {
                    ListViewItem item = (ListViewItem)m_items[curr.Id];
                    item.SubItems[0].Text = curr.Code;
                    item.SubItems[1].Text = curr.Name;
                    item.SubItems[2].Text = curr.Symbol;
                    item.SubItems[3].Text = curr.DecimalPlace.ToString();
                    item.SubItems[4].Text = curr.RoundingType.ToString();
                    item.SubItems[5].Text = curr.AutoAdjustment.ToString();
                    item.SubItems[6].Text = curr.LastUpdate.ToString("dd-MMM-yyyy hh:mm:ss");
                    item.SubItems[7].Text = curr.UserName;
                    item.SubItems[8].Text = curr.IsDefault.ToString();
                }
                else
                {
                    ListViewItem item = listViewRecords.Items.Add(curr.Code);
                    item.SubItems.Add(curr.Name);
                    item.SubItems.Add(curr.Symbol);
                    item.SubItems.Add(curr.DecimalPlace.ToString());
                    item.SubItems.Add(curr.RoundingType.ToString());
                    item.SubItems.Add(curr.AutoAdjustment.ToString());
                    item.SubItems.Add(curr.LastUpdate.ToString("dd-MMM-yyyy hh:mm:ss"));
                    item.SubItems.Add(curr.UserName);
                    item.SubItems.Add(curr.IsDefault.ToString());
                    item.Tag = curr;
                    m_items.Add(curr.Id, item);
                }
            }
        }

        public void SetMainForm(IMainForm mainform)
        {
            m_mainForm = mainform;
        }

        public void UpdateButton()
        {
            m_mainForm.UpdateButton(toolBarMain.Buttons);
        }

        public void Init()
        {
            if (b_init) { return; } b_init = true; 
            m_logic.Init();
            comboBoxRoundingType.DataSource = Enum.GetValues(typeof(RoundingType));
        }

        public void Refresh()
        {
            b_init = false;
            Init();
            m_logic.Refresh();
        }

        public void ClearForm()
        {
            tabPage1.Text = "";
            textBoxCode.Text = "";
            textBoxName.Text = "";
            textBoxSymbol.Text = "";
            numericUpDownAutoAdjustment.Value = 0;
            numericUpDownDecimalPlace.Value = 0;
            comboBoxRoundingType.SelectedItem = RoundingType.RoundingNormal;
            checkBoxIsDefault.Checked = false;
            statusBar1.Text = "";
            errorProvider1.SetError(textBoxCode, "");
            errorProvider1.SetError(textBoxName, "");
        }

        public void SetToReadOnly(bool ro)
        {
            textBoxCode.Enabled = !ro;
            textBoxName.Enabled = !ro;
            textBoxSymbol.Enabled = !ro;
            numericUpDownAutoAdjustment.Enabled = !ro;
            numericUpDownDecimalPlace.Enabled = !ro;
            comboBoxRoundingType.Enabled = !ro;
            checkBoxIsDefault.Enabled = !ro;
        }

        public void SetButtonEditMode(Premiere.Window.EditMode editMode)
        {
            toolBarButtonNew.Enabled = buttonNew.Enabled = ((editMode.Equals(EditMode.OK) || editMode.Equals(EditMode.Empty))) && m_mainForm.GetRules(this.Name).New;
            toolBarButtonSave.Enabled = buttonSave.Enabled = ((editMode.Equals(EditMode.Edit) || editMode.Equals(EditMode.Insert))) && m_mainForm.GetRules(this.Name).New;
            toolBarButtonEdit.Enabled = buttonEdit.Enabled = ((editMode.Equals(EditMode.OK))) && m_mainForm.GetRules(this.Name).Edit;
            toolBarButtonDelete.Enabled = buttonDelete.Enabled = ((editMode.Equals(EditMode.OK) || editMode.Equals(EditMode.Edit))) && m_mainForm.GetRules(this.Name).Delete;
            toolBarButtonPrint.Enabled = m_mainForm.GetRules(this.Name).Print;
        }

        public void RemoveItem(long id)
        {
            if (m_items.ContainsKey(id))
            {
                ListViewItem item = (ListViewItem)m_items[id];
                listViewRecords.Items.Remove(item);
            }
        }


        public void FocusToCode()
        {
            textBoxCode.Focus();
        }

        public bool IsValidSubject()
        {
            errorProvider1.SetError(textBoxCode, "");
            errorProvider1.SetError(textBoxName, "");
            bool a = textBoxCode.Text == string.Empty;
            bool b = textBoxName.Text == string.Empty;
            if (a) errorProvider1.SetError(textBoxCode, "Please fill Code");
            if (b) errorProvider1.SetError(textBoxName, "Please fill Name");
            return !a && !b;
        }


        #endregion

        private void decimalplacenumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            decimalFormatlabel.Text = m_decimalsample.ToString(Tools.CreateFormat(Convert.ToInt16(numericUpDownDecimalPlace.Value)));
        }

        private void autoadjustmentnumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            autoadjustmentlabel.Text = m_sample.ToString(Tools.CreateFormat(Convert.ToInt16(numericUpDownAutoAdjustment.Value)) + "0");
        }

        private void listViewRecords_DoubleClick(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            CurrencyDTO user = (CurrencyDTO)listViewRecords.SelectedItem.Tag;
            m_logic.UpdateSubject(user);
        }
    }
}